package conterlles

import (
	"exam12_11/models/mysql"
	"exam12_11/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func IfYouBNotKnowYouPassword(c *gin.Context) {
	tel := c.PostForm("tel")
	findUser := mysql.FindUser(tel)
	if findUser.ID == 0 {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "该用户不存在",
			Data:    nil,
		})
		return
	}
	password := c.PostForm("password")
	b, s := pkg.Tel(tel)
	if s != "" {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: s,
			Data:    nil,
		})
		return
	}
	if !b {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "手机号格式错误",
			Data:    nil,
		})
		return
	}

	d, t := pkg.Password(password)
	if t != "" {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: t,
			Data:    nil,
		})
		return
	}
	if !d {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "密码格式错误",
			Data:    nil,
		})
		return
	}

	password1 := password + "1234"
	password2 := pkg.Md5JM(password1)

	err := mysql.Update(password2, int(findUser.ID))
	if err != nil {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "修改密码失败",
			Data:    nil,
		})
		return
	}

	c.JSON(http.StatusOK, JsonRes{
		Code:    500,
		Message: "修改密码成功",
		Data:    nil,
	})

}
